//
//  HomePageTableViewCell.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/19.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "HomePageTableViewCell.h"
#import "MBProgressHUD+NJ.h"
@implementation HomePageTableViewCell

- (void)dealloc {
    
    [_bacImageView release];
    [_volLabel release];
    [_dataLabel release];
    [_timeLabel release];
    [_writerLabel release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.bacImageView = [[UIImageView alloc] init];
        
        self.volLabel = [[YHYBaseLabel alloc] init];
        self.volLabel.font = [UIFont systemFontOfSize:15];
        self.volLabel.textColor = [UIColor lightGrayColor];
        
        self.writerLabel = [[YHYBaseLabel alloc] init];
        self.writerLabel.font = [UIFont systemFontOfSize:15];
        self.writerLabel.textColor = [UIColor lightGrayColor];
        _writerLabel.textAlignment = NSTextAlignmentRight;
        
        self.timeLabel = [[YHYBaseLabel alloc] init];
        self.timeLabel.font = [UIFont systemFontOfSize:15];
        self.timeLabel.textColor = [UIColor lightGrayColor];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        
        self.stringLabel = [[YHYBaseLabel alloc] init];
        self.stringLabel.textAlignment = NSTextAlignmentLeft;
        
        self.dataLabel = [[YHYBaseLabel alloc] init];
        self.dataLabel.font = [UIFont systemFontOfSize:15];
        self.dataLabel.textAlignment = NSTextAlignmentLeft;
        self.dataLabel.textColor = [UIColor lightGrayColor];
        
        self.favoriteButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_favoriteButton addTarget:self action:@selector(favoriteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [_favoriteButton setImage:[[UIImage imageNamed:@"收藏前"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_shareButton setImage:[[UIImage imageNamed:@"分享"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                
        _stringLabel.numberOfLines = 0;
        
        [self.contentView addSubview:_bacImageView];
        [self.contentView addSubview:_volLabel];
        [self.contentView addSubview:_writerLabel];
        [self.contentView addSubview:_timeLabel];
        [self.contentView addSubview:_dataLabel];
        [self.contentView addSubview:_stringLabel];
        [self.contentView addSubview:_shareButton];
        [self.contentView addSubview:_favoriteButton];
        
        [_bacImageView release];
        [_volLabel release];
        [_dataLabel release];
        [_timeLabel release];
        [_writerLabel release];
        
        
        
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _labelHeight = [[self class]heightWithText:_stringLabel.text];
    
    [_bacImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(10);
        
        make.left.equalTo(self.contentView).offset(10);
        
        make.right.equalTo(self.contentView).offset(-10);
        
        make.height.equalTo(@300);
        
    }];
    
    
    [_volLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_bacImageView.mas_bottom).offset(5);
        
        make.left.equalTo(self.contentView).offset(10);
        
        make.width.equalTo(@100);
        
        make.height.equalTo(@20);
        
    }];
    
    [_writerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_volLabel.mas_top);
        
        make.right.equalTo(self.contentView).offset(-10);
        
        make.height.equalTo(@20);
        
        make.width.equalTo(@300);
        
        
    }];
    
    [_stringLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(_volLabel.mas_bottom).offset(10);
        
        make.left.equalTo(_volLabel.mas_left);
        
        make.right.equalTo(self.contentView).offset(-10);
        
        make.height.equalTo(@(_labelHeight));
        
    }];
    
    [_dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.contentView);
        
        make.bottom.equalTo(self.contentView).offset(-10);
        
        make.height.equalTo(@20);
        
        make.width.equalTo(@180);
        
    }];
    
    [_favoriteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.contentView).offset(-10);
        
        make.left.equalTo(self.contentView).offset(10);
        
        make.width.equalTo(@25);
        
        make.height.equalTo(@25);
        
    }];
    
    [_shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.contentView).offset(-10);
        
        make.left.equalTo(self.contentView).offset(60);
        
        make.width.equalTo(@25);
        
        make.height.equalTo(@25);
        
    }];
    
    // 刷新
    [self layoutIfNeeded];

    
}

- (void)favoriteButtonAction:(UIButton *)sender {
//    [self.delegate shareAction];
    
    if (_isHave == YES) {
        
        [[YHY_DataBaseTool shareDataBase] deleteHomePageDataModelTable:_dataModel.hp_content];
        
        [_favoriteButton setBackgroundImage:[[UIImage imageNamed:@"收藏前"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        [MBProgressHUD showMessage:@"取消收藏"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUD];
        
        });
        _isHave = NO;
        
    }else {
        
        [[YHY_DataBaseTool shareDataBase] insertDetailModelIntoTable:_dataModel];
        
        [_favoriteButton setBackgroundImage:[[UIImage imageNamed:@"收藏后"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [MBProgressHUD showMessage:@"已收藏"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUD];
            
        });
        _isHave = YES;
        
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"favorite" object:nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reload" object:nil];
    
}

- (void)shareButtonAction:(UIButton *)sender {
    
}


/**
 *  动态计算label高度
 */

+ (CGFloat)heightWithText:(NSString *)str {
    
    if (str == nil) {
        return 0;
    }
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    return rect.size.height + 120;
    
}

- (void)setDataModel:(HomePageDataModel *)dataModel {
    if (_dataModel != dataModel) {
        [_dataModel release];
        _dataModel = [dataModel retain];
    }
    
    /**< 收藏 */
//    BOOL select = [[YHY_DataBaseTool shareDataBase] selectDataBase:dataModel.hp_content];
    
    if ([[YHY_DataBaseTool shareDataBase] selectDataBase:dataModel.hp_content] == YES) {
        _isHave = YES;
        [_favoriteButton setBackgroundImage:[[UIImage imageNamed:@"收藏后"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }else {
        
        _isHave = NO;
        [_favoriteButton setBackgroundImage:[[UIImage imageNamed:@"收藏前"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
    }
    
    _dataLabel.text = dataModel.hp_makettime;
    _volLabel.text = dataModel.hp_title;
    _writerLabel.text = dataModel.hp_author;
    _stringLabel.text = dataModel.hp_content;
    [_bacImageView sd_setImageWithURL:[NSURL URLWithString:dataModel.hp_img_url]];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
